package org.example.mine.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.example.mine.config.LocalDateTimeUtil;
import org.example.mine.mapper.WithdrawMapper;
import org.example.mine.service.WithdrawService;
import org.example.model.domain.mine.ExpressWithdraw;
import org.example.model.vo.mine.WithdrawVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Slf4j
@Service
public class WithdrawServiceImpl implements WithdrawService {

    @Autowired
    private WithdrawMapper withdrawMapper;
    @Override
    public void addwithdrawrecord(ExpressWithdraw expressWithdraw) {
        String s = IdUtil.getSnowflakeNextIdStr();
        expressWithdraw.setOrderNo(s);
        expressWithdraw.setOverTime(new Date());
        withdrawMapper.addwithdrawrecord(expressWithdraw);
    }

    @Override
    public WithdrawVo getwithdrawrecord(Long id) {
        List<ExpressWithdraw> expressWithdraws = withdrawMapper.getwithdrawrecord(id);
        WithdrawVo withdrawVo = new WithdrawVo();
        if(expressWithdraws.size()!=0){
//            使用stream流中的Collectors.groupingBy进行分组排序
            Map<String,List<ExpressWithdraw>> map=expressWithdraws.stream().filter(e->e.getOverTime()!=null).collect(Collectors.groupingBy(e-> LocalDateTimeUtil.parse_yyyyMM(e.getOverTime())));
            log.info("map"+map);
            withdrawVo.setMap(map);
        }
        return withdrawVo;
    }

    @Override
    public ExpressWithdraw getwithdrawdetailbyid(Integer id) {
        return withdrawMapper.getwithdrawdetailbyid(id);
    }

    @Override
    public String getDailyIncome(Long registerIp) {
        DateTime begin = DateUtil.beginOfDay(new Date());
        DateTime end = DateUtil.endOfDay(new Date());
        BigDecimal num=withdrawMapper.getDailyIncome(registerIp,begin,end);
        return num.toString();
    }
}
